<?php

namespace App\Http\Controllers\V1\Api;

use App\Http\Requests\Api\MessageServer;
use App\Models\V1\Community;

use App\Models\V1\Message;
use App\Models\V1\MessageClass;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


class MessageController extends BaseController
{

    /**
     * 发布列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function List(Request $request)
    {
        $query = Message::query();
        $pageSize = $request->input('pageSize', 10);
        $order = $request->input('order', 'desc');
        $sort = $request->input('sort', 'created_at');
        $query->where('state', 1);
        $query->where('check_state', 1);
        $query->orderBy($sort, $order);
        $data = $query->paginate($pageSize);
        return ajax_output(200, "成功", $data);
    }


    /**
     * 我的发布
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function MyList(Request $request)
    {
        $id = auth('box')->id();
        $query = Message::query();
        $pageSize = $request->input('pageSize', 10);
        $order = $request->input('order', 'desc');
        $sort = $request->input('sort', 'created_at');

        if ($request->filled('check_state')) {
            $query->where('check_state', $request->input('check_state'));
        }

        $query->where('user_id', $id);
        $query->where('state', 1);

        $query->orderBy($sort, $order);
        $data = $query->paginate($pageSize);
        return ajax_output(200, "成功", $data);
    }





    /**
     * 用户发布信息
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function Add(MessageServer $request)
    {
        $request->validate('v1.create');
        $user = auth('box')->user();


        $title = $request->input('title');
        $message_class_id = $request->input('message_class_id');
        $describe = $request->input('describe');
        $thumb = $request->input('thumb');
        $contacts = $request->input('contacts');
        $mobile = $request->input('mobile');
        $priceu = $request->input('priceu');
        $community_name = Community::where('id', $user['community_id'])->value('name');
       $message_class_name =  MessageClass::where('id',$message_class_id)->value('title');


        $model = new Message();
        try {
            DB::beginTransaction();
            $model->create([
                'message_class_id' => $message_class_id,
                'title' => $title,
                'describe' => $describe,
                'thumb' => $thumb,
                'contacts' => $contacts,
                'mobile' => $mobile,
                'priceu' => $priceu,

                'user_id' => $user['id'],
                'username' => $user['username'],
                'community_id' => $user['community_id'],
                'community_name' => $community_name,
                'message_class_name' => $message_class_name,
            ]);
            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            return ajax_output(300, "数据异常:" . $e->getMessage());
        }
        return ajax_output(200, "成功");
    }


}
